package menuService

import (
	"fmt"
	menuDto "news_for_deepseek_interface/dto/admin/system/menu"
	"news_for_deepseek_interface/model"
	adminModel "news_for_deepseek_interface/model/admin"
	"time"
)

// 插入菜单
func Create(params menuDto.CreateRequest) (err error) {

	db := model.GetInstance().GetDB().Model(&adminModel.AdminAuthRuleStruct{})

	level := 0
	if params.ParentId != 0 {
		var parentMenu adminModel.AdminAuthRuleStruct
		if err = db.Where("id = ?", params.ParentId).First(&parentMenu).Error; err != nil {
			return fmt.Errorf("父级菜单不存在")
		}
		level = parentMenu.Level + 1
	}

	if err = db.Create(map[string]interface{}{
		"parent_id":   params.ParentId,
		"level":       level,
		"title":       params.Title,
		"name":        params.Name,
		"path":        params.Path,
		"component":   params.Component,
		"rank":        params.Rank,
		"show_link":   params.ShowLink,
		"icon":        params.Icon,
		"show_parent": params.ShowParent,
		"menu_type":   params.MenuType,
		"created_at":  time.Now().Format("2006-01-02 15:04:05"),
		"updated_at":  time.Now().Format("2006-01-02 15:04:05"),
	}).Error; err != nil {
		return fmt.Errorf("插入菜单失败: %v", err)
	}
	return nil
}
